# 设置CodeQL数据库路径
$dbPath = "C:\code\totalCode_DB\oh_2d_db"

Write-Host "配置CodeQL数据库..." -ForegroundColor Green

# 1. 检查数据库是否存在
if (Test-Path $dbPath) {
    Write-Host "✓ 数据库存在: $dbPath" -ForegroundColor Green
    
    # 2. 验证数据库
    Write-Host "`n验证数据库..." -ForegroundColor Yellow
    try {
        $dbInfo = codeql database info $dbPath 2>&1 | Select-Object -First 15
        if ($LASTEXITCODE -eq 0) {
            Write-Host "✓ 数据库验证成功" -ForegroundColor Green
            Write-Host "`n数据库信息:" -ForegroundColor Cyan
            $dbInfo | ForEach-Object { 
                Write-Host "  $_" -ForegroundColor Gray 
            }
        } else {
            Write-Host "⚠ 数据库可能损坏或不完整" -ForegroundColor Yellow
            Write-Host "响应: $dbInfo" -ForegroundColor Gray
        }
    } catch {
        Write-Host "⚠ 无法验证数据库（CodeQL CLI可能未安装）" -ForegroundColor Yellow
        Write-Host "错误: $_" -ForegroundColor Gray
    }
    
    # 3. 检查数据库大小
    Write-Host "`n检查数据库大小..." -ForegroundColor Yellow
    try {
        $dbSize = (Get-ChildItem -Path $dbPath -Recurse -ErrorAction SilentlyContinue | 
                   Measure-Object -Property Length -Sum).Sum / 1GB
        Write-Host "✓ 数据库大小: $([math]::Round($dbSize, 2)) GB" -ForegroundColor Green
    } catch {
        Write-Host "⚠ 无法计算数据库大小" -ForegroundColor Yellow
    }
    
    # 4. 设置环境变量
    Write-Host "`n设置环境变量..." -ForegroundColor Yellow
    $env:CODEQL_DB_PATH = $dbPath
    Write-Host "✓ CODEQL_DB_PATH = $dbPath" -ForegroundColor Green
    Write-Host "  注意: 此设置仅在当前PowerShell会话中有效" -ForegroundColor Gray
    Write-Host "  永久设置: 系统环境变量 → 添加CODEQL_DB_PATH" -ForegroundColor Gray
    
    # 5. 更新配置文件（可选）
    Write-Host "`n更新配置文件..." -ForegroundColor Yellow
    $configFile = "data-service\src\main\resources\application.yml"
    if (Test-Path $configFile) {
        $content = Get-Content $configFile -Raw
        # 转义路径中的反斜杠用于YAML
        $escapedPath = $dbPath.Replace('\', '\\')
        
        # 替换database-path配置
        if ($content -match 'database-path:\s*\$\{CODEQL_DB_PATH:[^}]+\}') {
            $newContent = $content -replace 'database-path:\s*\$\{CODEQL_DB_PATH:[^}]+\}', 
                "database-path: ``${CODEQL_DB_PATH:$escapedPath}"
            Set-Content -Path $configFile -Value $newContent -NoNewline
            Write-Host "✓ 配置文件已更新: $configFile" -ForegroundColor Green
        } else {
            Write-Host "⚠ 配置文件格式可能已更改，请手动更新" -ForegroundColor Yellow
        }
    } else {
        Write-Host "⚠ 配置文件不存在: $configFile" -ForegroundColor Yellow
    }
    
    Write-Host "`n配置完成！" -ForegroundColor Green
    Write-Host "`n下一步:" -ForegroundColor Yellow
    Write-Host "  1. 启动Data Service: cd data-service && mvn spring-boot:run" -ForegroundColor Gray
    Write-Host "  2. 测试API: .\test-codeql-api.ps1" -ForegroundColor Gray
    Write-Host "  3. 运行检查: .\check-codeql.ps1" -ForegroundColor Gray
    
} else {
    Write-Host "✗ 数据库不存在: $dbPath" -ForegroundColor Red
    Write-Host "`n请检查路径是否正确" -ForegroundColor Yellow
    Write-Host "当前路径: $PWD" -ForegroundColor Gray
    
    # 尝试查找可能的路径
    Write-Host "`n搜索可能的CodeQL数据库..." -ForegroundColor Yellow
    $possiblePaths = @(
        "C:\code\totalCode_DB\oh_2d_db",
        ".\codeql-database",
        ".\OHCode\codeql-database",
        "..\codeql-database"
    )
    
    foreach ($path in $possiblePaths) {
        if (Test-Path $path) {
            Write-Host "  ✓ 找到: $path" -ForegroundColor Green
        }
    }
}

Write-Host "`n" -NoNewline

